/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package radio_GUI;

import javax.persistence.EntityManager;
import javax.swing.JOptionPane;
import model.Artist;
import model.DBManager;
import model.Musicgroup;
import model.Playlist;

/**
 *
 * @author user
 */
public class MusicGroup extends javax.swing.JFrame {

    /**
     * Creates new form MusicGroup
     */
    private final EntityManager em;//O entity manager
    
    public MusicGroup() {
    
        this.em = DBManager.getEm();//.em;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        MusicGQ = java.beans.Beans.isDesignTime() ? null : DBManager.getEm().createQuery("SELECT m FROM Musicgroup m");
        MusicGL = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(MusicGQ.getResultList());
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        Insert = new javax.swing.JButton();
        Delete = new javax.swing.JButton();
        Modify = new javax.swing.JButton();
        EXIT = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));

        jLabel1.setText("Πίνακας Συγκροτημάτων");
        jLabel1.setToolTipText("");
        jLabel1.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jLabel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabel1.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, MusicGL, jTable1);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${name}"));
        columnBinding.setColumnName("Όνομα Συγκρ.");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${formationdate}"));
        columnBinding.setColumnName("Δημιουργία");
        columnBinding.setColumnClass(java.util.Date.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane1.setViewportView(jTable1);

        Insert.setText("Νέο");
        Insert.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        Insert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                InsertActionPerformed(evt);
            }
        });

        Delete.setText("Διαγραφή");
        Delete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteActionPerformed(evt);
            }
        });

        Modify.setText("Μεταβολή");
        Modify.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ModifyActionPerformed(evt);
            }
        });

        EXIT.setText("Πίσω");
        EXIT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EXITActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 475, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Insert)
                        .addGap(112, 112, 112)
                        .addComponent(Modify)
                        .addGap(174, 174, 174)
                        .addComponent(Delete))
                    .addComponent(EXIT, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(81, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Insert)
                    .addComponent(Delete)
                    .addComponent(Modify))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 78, Short.MAX_VALUE)
                .addComponent(EXIT)
                .addGap(45, 45, 45))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void InsertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_InsertActionPerformed
        // TODO add your handling code here:
        
        //νέο αντικείμενο
        //Musicgroup m = new Musicgroup();
        //em.persist(m);
        
        //παιρνάω αποτελέσματα σε EditMusicGroupForm
        EditMusicGroupForm emg =new EditMusicGroupForm();
        emg.setVisible(true);
        
        this.dispose();
        //new EditMusicGroupForm().setVisible(true);// νέο αντικείμενο

        
        //εισαγωγη γραμμής σε λίστα συγκροτήματα-προσθέτω στην MusicGL object m
        //ενημερώνεατι καθε φορα η λίστα απο πίνακα
        //MusicGL.add(m);
    }//GEN-LAST:event_InsertActionPerformed

    private void ModifyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ModifyActionPerformed
        // TODO add your handling code here:
        //epilegoume grami sto jTable
        Musicgroup m = new model.Musicgroup();
        int index = jTable1.convertRowIndexToModel(jTable1.getSelectedRow()); 
        if(index>=0) {   //Έλεγχος για επιλογή Musicgroup
             //pernoume to antikimeno MusicGroup gia allagi stoixeiwn
            m = MusicGL.get(index); 
            // emfanizoume edit gia metavoli stoixeiwn Music Group
            EditMusicGroupForm emg =new EditMusicGroupForm(m,2);
            emg.setVisible(true);
            this.dispose();    
        }else { 
            //Εμφάνιση μηνύματος
            JOptionPane.showMessageDialog(this,
                                "Επιλογή γραμμής από τον πίνακα");
    }//GEN-LAST:event_ModifyActionPerformed
    }
    private void EXITActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EXITActionPerformed
       
      // TODO add your handling code here:
        
        //epistrofh se kentiko menu
        this.dispose(); 
        new MainMenu().setVisible(true);
        
        System.out.println("Exit Edit Music Group Form to main menu");
  
    }//GEN-LAST:event_EXITActionPerformed

    private void DeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteActionPerformed
          // apothikeuse tin grami pou einai epilegmeni sto jTable
       model.Musicgroup m = new model.Musicgroup();
       int index = jTable1.convertRowIndexToModel(jTable1.getSelectedRow()); //apothikevoume tin grammi pou epilegoume sto jTable
 
     
       if(index>=0 && index < jTable1.getRowCount()) { 
           m=MusicGL.get(index); //pernoume to antikimeno MusicGL gia diagrafh stoixeiwn
        //παιρνάω αποτελέσματα σε EditMusicGroupForm
           EditMusicGroupForm emg =new EditMusicGroupForm(m,1);
           emg.setVisible(true);
           this.dispose();
       }else { 
        JOptionPane.showMessageDialog(this,
                         "Παρακαλώ επιλέξτε γραμμή απο τον πίνακα.");//Εμφάνιση μηνύματος
      }
      
        
    }//GEN-LAST:event_DeleteActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MusicGroup.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MusicGroup.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MusicGroup.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MusicGroup.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DBManager.openConnection();//Ksekina ton entity manager gia test                
                new MusicGroup().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Delete;
    private javax.swing.JButton EXIT;
    private javax.swing.JButton Insert;
    private javax.swing.JButton Modify;
    private java.util.List<Musicgroup> MusicGL;
    private javax.persistence.Query MusicGQ;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    //EntityManager em;

    //private static class MusicGL {

        //public MusicGL(MusicGroup aThis, Musicgroup MusicgroupDel, boolean b) {
        //}

       // private void setVisible(boolean b) {
       //     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
      //  }
    //}
}

